package com.jeff.client;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
 
public final class ChooseContact extends Activity 
{ 
 
    public static final String TAG = "ContactManager"; 
 
    private Button mAddAccountButton; 
    private ListView mContactList; 
    private boolean mShowInvisible; 
 
    /** 
     * Called when the activity is first created. Responsible for initializing the UI. 
     */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        Log.v(TAG, "Activity State: onCreate()"); 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.contact_manager); 
 
        // Obtain handles to UI objects 
        mAddAccountButton = (Button) findViewById(R.id.choosePhoneOK); 
        mContactList = (ListView) findViewById(R.id.contactList); 
 
 
        mAddAccountButton.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) { 
                Log.d(TAG, "mAddAccountButton clicked"); 
                finish();
            } 
        }); 
        
        /*mContactList.setOnItemClickListener(new OnItemClickListener() { 
            public void onItemClick(AdapterView<?> parent, View view, 
                int position, long id) { 
              // When clicked, show a toast with the TextView text 
              Toast.makeText(getApplicationContext(), ((TextView) view).getText(), 
                  Toast.LENGTH_SHORT).show(); 
            } 
          }); */

        // Populate the contact list 
        populateContactList(); 
    } 
 
    /** 
     * Populate the contact list based on account currently selected in the account spinner. 
     */ 
    private void populateContactList() { 
        // Build adapter with contact entries 
        Cursor cursor = getContacts(); 
        String[] fields = new String[] { 
                ContactsContract.Data.DISPLAY_NAME 
        }; 
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor, 
                fields, new int[] {R.id.contactEntryText}); 
        mContactList.setAdapter(adapter); 
    } 
 
    /** 
     * Obtains the contact list for the currently selected account. 
     * 
     * @return A cursor for for accessing the contact list. 
     */ 
    private Cursor getContacts() 
    { 
        // Run query 
        Uri uri = ContactsContract.Contacts.CONTENT_URI; 
        String[] projection = new String[] { 
                ContactsContract.Contacts._ID, 
                ContactsContract.Contacts.DISPLAY_NAME 
        }; 
        String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + 
                (mShowInvisible ? "0" : "1") + "'"; 
        String[] selectionArgs = null; 
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; 
 
        return managedQuery(uri, projection, selection, selectionArgs, sortOrder); 
    } 
 
}
